package com.orange.quickback.system.config.oath2config;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.FilterInvocation;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class CustomAccessDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication auth, Object obj, Collection<ConfigAttribute> ca){
        if(obj instanceof FilterInvocation){
            FilterInvocation fi = (FilterInvocation)obj;
            if(fi.getRequest().getRequestURI().equalsIgnoreCase("/register")){
                return;
            }
        }
        Collection<? extends GrantedAuthority> auths = auth.getAuthorities();
        for(ConfigAttribute configAttribute:ca){
            if("ROLE_visitor".equalsIgnoreCase(configAttribute.getAttribute())&&auth instanceof UsernamePasswordAuthenticationToken){
                return;
            }
            for(GrantedAuthority authority:auths){
                if(configAttribute.getAttribute().equals(authority.getAuthority())){
                    return;
                }
            }
        }
        throw new AccessDeniedException("权限不足");
    }

    @Override
    public boolean supports(ConfigAttribute configAttribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
